package com.xian.util;

import org.apache.commons.lang3.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期时间工具类
 * jdk8建议
 * SimpleDateFormat   ->  建议使用 DateTimeFormatter
 * Calendar -> 建议使用 LocalDateTime
 * Date -> 建议使用 Instant
 * <p>
 * /*
 * LocalDate与Date转化
 * // 01. java.util.Date --> java.time.LocalDateTime
 * public void UDateToLocalDateTime() {
 * java.util.Date date = new java.util.Date();
 * Instant instant = date.toInstant();
 * ZoneId zone = ZoneId.systemDefault();
 * LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
 * }
 * <p>
 * // 02. java.util.Date --> java.time.LocalDate
 * public void UDateToLocalDate() {
 * java.util.Date date = new java.util.Date();
 * Instant instant = date.toInstant();
 * ZoneId zone = ZoneId.systemDefault();
 * LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
 * LocalDate localDate = localDateTime.toLocalDate();
 * }
 * <p>
 * // 03. java.util.Date --> java.time.LocalTime
 * public void UDateToLocalTime() {
 * java.util.Date date = new java.util.Date();
 * Instant instant = date.toInstant();
 * ZoneId zone = ZoneId.systemDefault();
 * LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
 * LocalTime localTime = localDateTime.toLocalTime();
 * }
 * <p>
 * <p>
 * // 04. java.time.LocalDateTime --> java.util.Date
 * public void LocalDateTimeToUdate() {
 * LocalDateTime localDateTime = LocalDateTime.now();
 * ZoneId zone = ZoneId.systemDefault();
 * Instant instant = localDateTime.atZone(zone).toInstant();
 * java.util.Date date = Date.from(instant);
 * }
 * <p>
 * <p>
 * // 05. java.time.LocalDate --> java.util.Date
 * public void LocalDateToUdate() {
 * LocalDate localDate = LocalDate.now();
 * ZoneId zone = ZoneId.systemDefault();
 * Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
 * java.util.Date date = Date.from(instant);
 * }
 * <p>
 * // 06. java.time.LocalTime --> java.util.Date
 * public void LocalTimeToUdate() {
 * LocalTime localTime = LocalTime.now();
 * LocalDate localDate = LocalDate.now();
 * LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
 * ZoneId zone = ZoneId.systemDefault();
 * Instant instant = localDateTime.atZone(zone).toInstant();
 * java.util.Date date = Date.from(instant);
 * }
 */
public class MyDateUtils {

    /**
     * java.time.LocalDateTime --> java.util.Date
     *
     * @param sDate 参数请传入日期不符 如： 2019-12-23T23:11:34   或  2019-12-23 23:11:34
     *              后面还需要改进，如判断非空，判断传入格式等
     *              <p>
     *              使用方式:
     *              Date date = MyDateUtils.localDateTime2Date("2015-09-23T21:26:30");
     *              System.out.println(date);
     *              <p>
     *              Date date2 = MyDateUtils.localDateTime2Date("2015-09-26 23:28:30");
     *              *       System.out.println(date2);
     *              传入毫秒值也可以
     *              Date date3 = MyDateUtils.localDateTime2Date("2015-09-23 21:28:30.769");
     *              System.out.println(date3);
     *              System.out.println(date3.getTime());
     */
    public static Date localDateTime2Date(String sDate) {
        //替换中间空格为  T
        if (StringUtils.isNotBlank(sDate)) {
            sDate = StringUtils.trim(sDate);
            sDate = sDate.replace(" ", "T");
        }
        LocalDateTime localDateTime = LocalDateTime.parse(sDate);
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    /**
     * java.time.LocalDate --> java.util.Date
     * 输入内容如： 2019-12-23   2021-01-20
     * @param sDate 参数请传入日期不符 如： 2019-12-23   2021-01-20
     *              后面还需要改进，如判断非空，判断传入格式等
     *              <p>
     *              使用方式:
     *              Date date = MyDateUtils.localDate2Date("2019-06-08");
     *              System.out.println(date);
     */
    public static Date localDate2Date(String sDate) {
        LocalDate localDate = LocalDate.parse(sDate);
        Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    /**
     * Date 转格YYYY-MM-dd HH:mm:ss式字符串输出
     * @param date
     * @return
     */
    public static String date2string(Date date){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
        Instant instant = date.toInstant();
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        String sDate = zonedDateTime.format(dateTimeFormatter);
        return sDate;
    }

    /**
     * 获取13位毫秒时间戳
     * 这里保持第一位数是1
     * @return
     */
    public static long getRandomTimestamp(){
        //long timestamp = System.currentTimeMillis();
        double r = Math.random(); //获取到数据如： 0.6681125840002072  18个字符
        String s = new StringBuilder(String.valueOf(r)).reverse().toString();
        String ts = s.substring(0, 12); //取12位时间戳
        long timestamp = Long.parseLong(1+ts);
        return timestamp;
    }

    //时间戳变常用格式 System.out.println(MyDateUtils.date2string(new Date(timestamp)));


}
